package org.example;

import org.jetbrains.annotations.NotNull;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.util.Locale;

/*
用来获取前端展示显示屏的密码和时间戳。
cmd:java -jar .\MqttTest.jar CLT33927491500 PlayBox d7e6d99ee838444eabb370347c487fa1
响应：MqttConnectParams(userName=COLORLIGHT10-PlayBox_CLT33927491500, password=5022b32318d8ec71a2516206b76bdaa89e49cedf, clientId=CLT33927491500|securemode=3,signmethod=hmacsha1,timestamp=1713339532|, timestamp=1713339532, deviceName=CLT33927491500)
*/
public class ScreenUpCloud {
    @NotNull
    public static final ScreenUpCloud INSTANCE = new ScreenUpCloud();
    @NotNull
    public static final String HAMC_SHA1 = "HmacSHA1";
    @NotNull
    public static final String CLIENT_ID_TEMP = "|securemode=3,signmethod=hmacsha1,timestamp=";
    @NotNull
    public static final String ENV_PREFIX = "COLORLIGHT10";

    private ScreenUpCloud() {
    }

    private final byte[] hmacSha1(String key, String data) {
        try {
            byte[] var10002 = key.getBytes();
            SecretKeySpec signKey = new SecretKeySpec(var10002, "HmacSHA1");
            Mac mac = Mac.getInstance("HmacSHA1");
            mac.init((Key)signKey);
            byte[] var10001 = data.getBytes();
            return mac.doFinal(var10001);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private final String bytesToHex(byte[] bytes) {
        String hexArray = "0123456789abcdef";
        char[] hexChars = new char[bytes.length * 2];
        int j = 0;

        for(int var5 = bytes.length; j < var5; ++j) {
            int v = bytes[j] & 255;
            hexChars[j * 2] = hexArray.charAt(v >>> 4);
            hexChars[j * 2 + 1] = hexArray.charAt(v & 15);
        }

        return new String(hexChars);
    }

    @NotNull
    public final MqttConnectParams generateMqttParams(@NotNull String deviceName, @NotNull String productKey, @NotNull String deviceSecret) {
        long timestamp = System.currentTimeMillis();
        long seconds = timestamp / (long)1000;
        String clientId = deviceName + "|securemode=3,signmethod=hmacsha1,timestamp=" + seconds + '|';
        String username = "COLORLIGHT10-" + productKey + '_' + deviceName;
        String hmacIn = "clientId" + deviceName + "deviceName" + productKey + '_' + deviceName + "productKeyCOLORLIGHT10timestamp" + seconds;
        byte[] var11 = this.hmacSha1(deviceSecret, hmacIn);
        if (var11 != null) {
            byte[] it = var11;
            String var15 = INSTANCE.bytesToHex(it);
            Locale var16 = Locale.getDefault();
            String var10000 = var15.toLowerCase(var16);
            String password = var10000;
            return new MqttConnectParams(username, password, clientId, seconds, deviceName);
        } else {
            return new MqttConnectParams(username, "", clientId, seconds, deviceName);
        }
    }

    public static final String main(@NotNull String[] args) {
        MqttConnectParams params = INSTANCE.generateMqttParams(args[0], args[1], args[2]);
        System.out.println(params);
        return params.password+" "+params.timestamp;
    }

    public static final class MqttConnectParams {
        @NotNull
        private final String userName;
        @NotNull
        private final String password;
        @NotNull
        private final String clientId;
        private final long timestamp;
        @NotNull
        private final String deviceName;

        public MqttConnectParams(@NotNull String userName, @NotNull String password, @NotNull String clientId, long timestamp, @NotNull String deviceName) {
            this.userName = userName;
            this.password = password;
            this.clientId = clientId;
            this.timestamp = timestamp;
            this.deviceName = deviceName;
        }

        @NotNull
        public final String getUserName() {
            return this.userName;
        }

        @NotNull
        public final String getPassword() {
            return this.password;
        }

        @NotNull
        public final String getClientId() {
            return this.clientId;
        }

        public final long getTimestamp() {
            return this.timestamp;
        }

        @NotNull
        public final String getDeviceName() {
            return this.deviceName;
        }

        @NotNull
        public String toString() {
            return "MqttConnectParams(userName=" + this.userName + ", password=" + this.password + ", clientId=" + this.clientId + ", timestamp=" + this.timestamp + ", deviceName=" + this.deviceName + ')';
        }

    }
}
